import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'dart:io';

import 'package:get/get.dart';
import 'package:signage/app/data/models/member_group_model.dart';
import 'package:signage/app/data/models/member_info_item_model.dart';
import 'package:signage/l10n/l10n.dart';
import 'package:signage/widgets/custom_appbar.dart';
import 'package:signage/widgets/device_list_item.dart';

import '../controllers/cast_record_detail_controller.dart';

class CastRecordDetailView extends GetView<CastRecordDetailController> {
  const CastRecordDetailView({super.key});
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 3,
      child: Scaffold(
      backgroundColor: const Color.fromRGBO(243, 244, 246, 1),
        appBar: CustomAppBar(
          backgroundColor: Colors.white,
          title: AppLocalizations.of(context)!.translate('details'),
          bottom: TabBar(
            padding: EdgeInsets.zero,
            labelPadding: EdgeInsets.symmetric(horizontal: 0),
            labelColor: Colors.black,
            unselectedLabelColor: Colors.black,
            indicatorColor: Colors.black,
            indicatorWeight: 2,
            tabs: [
              Tab(text: AppLocalizations.of(context)!.translate('devices')),
              Tab(text: AppLocalizations.of(context)!.translate('member_list')),
              Tab(text: AppLocalizations.of(context)!.translate('template')),
            ],
          ),
        ),
        body: TabBarView(
          children: [
            // 设备页面
            SingleChildScrollView(
              child: Padding(
                padding: EdgeInsets.all(16.w),
                child: Container(
                  padding: EdgeInsets.all(16.w),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(12.r),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Row(
                        children: [
                          Text(
                            "${AppLocalizations.of(context)!.translate('device_count')}: ${controller.record?.deviceInfos.length ?? 0}",
                            style: TextStyle(fontSize: 13.w),
                          ),
                          SizedBox(width: 16.w),
                          Text(
                            "${AppLocalizations.of(context)!.translate('success')}: ${controller.record?.successCount ?? 0}",
                            style: TextStyle(
                              fontSize: 13.w,
                              color: Colors.green,
                            ),
                          ),
                          SizedBox(width: 16.w),
                          Text(
                            "${AppLocalizations.of(context)!.translate('failed')}: ${controller.record?.failureCount ?? 0}",
                            style: TextStyle(
                              fontSize: 13.w,
                              color: Colors.red,
                            ),
                          ),
                        ],
                      ),
                      SizedBox(height: 10.w),
                      ...?controller.record?.deviceInfos.map((cachedInfo) {
                        return Padding(
                          padding: EdgeInsets.only(bottom: 8.w),
                          child: DeviceListItem(
                            cachedInfo: cachedInfo,
                            connectionStateStream: null,
                            showDeleteButton: false,
                            isView: true,
                            onRemove: (id) {},
                          ),
                        );
                      }),
                    ],
                  ),
                ),
              ),
            ),
            // 名单页面
            Padding(
              padding: EdgeInsets.all(16.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.all(16.w),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(12.r),
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            controller.currentGroup.value?.groupName ?? AppLocalizations.of(context)!.translate('none'),
                            overflow: TextOverflow.ellipsis,
                          ),
                          5.verticalSpace,
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                  " ${AppLocalizations.of(context)!.translate('member_count')} ${controller.currentGroup.value?.memberList?.length ?? 0}"),
                            ],
                          ),
                          Expanded(
                            child: ListView.builder(
                              shrinkWrap: false,
                              physics: AlwaysScrollableScrollPhysics(),
                              itemCount: controller
                                      .currentGroup.value?.memberList?.length ??
                                  0,
                              itemBuilder: (_, index) => _buildMemberCell(
                                  controller
                                      .currentGroup.value?.memberList?[index]),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
            // 模板页面
            Padding(
              padding: EdgeInsets.all(16.w),
              child: SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      padding: EdgeInsets.all(16.w),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(12.r),
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                  controller.record?.castMode == 0
                                      ? AppLocalizations.of(context)!.translate('ab_same')
                                      : controller.record?.castMode == 1
                                          ? AppLocalizations.of(context)!.translate('ab_different')
                                          : controller.record?.castMode == 2
                                              ? AppLocalizations.of(context)!.translate('a_only')
                                              : AppLocalizations.of(context)!.translate('b_only'),
                                  style: TextStyle(fontSize: 13.w, color: Colors.black87)),
                            ],
                          ),
                          SizedBox(height: 15.w),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              if (controller.record?.castMode != 3)
                                Container(
                                  width: 140.w,
                                  height: 84.w,
                                  decoration: BoxDecoration(
                                    color: Colors.grey[200],
                                    borderRadius: BorderRadius.circular(8.r),
                                    image: controller.record?.templateA != null
                                        ? DecorationImage(
                                            image: controller
                                                    .record!.templateA!.filePath
                                                    .startsWith('assets/')
                                                ? AssetImage(controller.record!
                                                    .templateA!.filePath)
                                                : FileImage(File(controller
                                                        .record!
                                                        .templateA!
                                                        .filePath))
                                                    as ImageProvider,
                                            fit: BoxFit.cover,
                                          )
                                        : null,
                                  ),
                                  child: controller.record?.templateA == null
                                      ? Center(
                                          child: Text(
                                            AppLocalizations.of(context)!.translate('template_a'),
                                            style: TextStyle(
                                              color: Colors.grey[600],
                                              fontSize: 13.sp,
                                            ),
                                          ),
                                        )
                                      : null,
                                ),
                              if (controller.record?.castMode != 2)
                                Container(
                                  width: 140.w,
                                  height: 84.w,
                                  decoration: BoxDecoration(
                                    color: Colors.grey[200],
                                    borderRadius: BorderRadius.circular(8.r),
                                    image: controller.record?.templateB != null
                                        ? DecorationImage(
                                            image: controller
                                                    .record!.templateB!.filePath
                                                    .startsWith('assets/')
                                                ? AssetImage(controller.record!
                                                    .templateB!.filePath)
                                                : FileImage(File(controller
                                                        .record!
                                                        .templateB!
                                                        .filePath))
                                                    as ImageProvider,
                                            fit: BoxFit.cover,
                                          )
                                        : null,
                                  ),
                                  child: controller.record?.templateB == null
                                      ? Center(
                                          child: Text(
                                            AppLocalizations.of(context)!.translate('template_b'),
                                            style: TextStyle(
                                              color: Colors.grey[600],
                                              fontSize: 13.sp,
                                            ),
                                          ),
                                        )
                                      : null,
                                ),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildMemberCell(MemberItem? info) => Column(
        children: [
          SizedBox(height: 10.w),
          ClipRRect(
            borderRadius: BorderRadius.circular(12.r),
            child: Container(
              color: const Color(0xFFF0FDF7),
              padding: EdgeInsets.all(15.w),
              child: ListView.builder(
                shrinkWrap: true,
                scrollDirection: Axis.vertical,
                physics: NeverScrollableScrollPhysics(),
                itemCount: info?.memberInfoList?.length ?? 0,
                itemBuilder: (_, index) =>
                    _buildMemberInfoCell(info?.memberInfoList?[index]),
              ),
            ),
          ),
        ],
      );

  Widget _buildMemberInfoCell(MemberInfoItem? info) => Row(
        children: [
          Text(
            info?.keyName ?? "暂无",
            style: TextStyle(
              fontSize: 13.w,
              color: Colors.green,
            ),
          ),
          Text(
            ":",
            style: TextStyle(
              color: Colors.green,
            ),
          ),
          Text(
            info?.value ?? "暂无",
            style: TextStyle(
              fontWeight: FontWeight.bold,
              color: Colors.green,
            ),
          ),
        ],
      );
}
